package com.ahzx.banner.controller;

import com.ahzx.utils.FileToSftp;
import com.ahzx.utils.ServiceException;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

/**
 * banner图片控制层
 *
 * @Author syg
 * @Date 2021/4/27 11:07
 * @Desc To be or not to be,that is a question.
 */
@RestController
@RequestMapping("/banner/image")
public class BannerImageController extends BaseController {

    @Autowired
    private FileToSftp fileToSftp;
    @Value("${sftp.host}")
    private String sftpHost;
    @Value("${sftp.bannerPath}")
    private String bannerPath;

    /**
     * banner图上传
     */
    @Log(title = "banner图上传", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    @SuppressWarnings("unchecked")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return AjaxResult.error("上传banner图片异常，稍后请重试！");
        }
        HashMap map = new HashMap();
        Session session = null;
        ChannelSftp sftp = null;
        try {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile(), file);
//            ftp = new Ftp(host, port, username, password);
//            ftp.cd(bannerPath);
//            ftp.upload(bannerPath, FileUtil.file(RuoYiConfig.getProfile() + "/banner" + avatar));
            session = fileToSftp.getSftpSession(sftpHost);
            sftp = fileToSftp.getSftpClient(session);
            //创建SFTP文件夹
            fileToSftp.mkdirFile(sftp, bannerPath + "/", true);
            String sftpFilePath = bannerPath + "/" + avatar;
            fileToSftp.uploadFile(sftp, RuoYiConfig.getProfile() + "/" + avatar, sftpFilePath);
            logger.info("sftp服务器文件上传成功 - {}", sftpFilePath);
            map.put("imageName", avatar.substring(avatar.lastIndexOf("/") + 1));
        } catch (Exception e) {
            logger.error("sftp图片上传异常 {}", e.getMessage());
            throw new ServiceException("sftp图片上传失败！");
        } finally {
            if (session != null) {
                session.disconnect();
            }
            if (sftp != null) {
                sftp.disconnect();
            }
        }
//        String bannerAllPath = bannerPath + avatar.substring(avatar.lastIndexOf("/") + 1);
        return AjaxResult.success(map);
    }

    /**
     * banner图删除
     */
    @Log(title = "banner图删除", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam("bannerimage") MultipartFile file) throws IOException {
        return AjaxResult.success();
    }
}
